package com.example.module.web;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;

import com.example.entity.Const;

public class CustomWebView extends WebView {
	private static final String TAG = "CustomWebView";
	
	public boolean isPageLoading = false;
	private OnScrollListener scrollListener;
	
	public interface OnScrollListener {
		public void onScroll(int l, int t, int oldl, int oldt);
	}
	
	public CustomWebView(Context context) {
		super(context);
		initialize();
	}
	public CustomWebView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initialize();
	}
	public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initialize();
	}

	public void initialize() {
		requestFocus(View.FOCUS_DOWN);
		setHorizontalScrollBarEnabled(false);
		setVerticalScrollBarEnabled(true);
		setVerticalScrollbarOverlay(true);
		
		WebSettings webSettings = this.getSettings();
		if (webSettings != null) {
			webSettings.setRenderPriority(RenderPriority.HIGH);
			webSettings.setLoadsImagesAutomatically(true);
			webSettings.setBuiltInZoomControls(true); 
			webSettings.setSupportZoom(true); //상세 웹뷰에서만 사용
			webSettings.setJavaScriptEnabled(true);
			webSettings.setLoadWithOverviewMode(true);
			webSettings.setUseWideViewPort(true);

			webSettings.setDomStorageEnabled(true);
			webSettings.setDatabaseEnabled(true);
			webSettings.setDatabasePath(Const.WEB_DATABASE_PATH);
			webSettings.setAllowFileAccess(true);
			webSettings.setSupportMultipleWindows(false);
			webSettings.setAppCacheEnabled(true);
			webSettings.setAppCachePath(Const.WEB_CACHE_PATH);
			webSettings.setAppCacheMaxSize(10 * 1024 * 1024);
		}
	}
	
	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		if (scrollListener != null)
			scrollListener.onScroll(l, t, oldl, oldt);
		
		super.onScrollChanged(l, t, oldl, oldt);
	}

	public OnScrollListener getScrollListener() {
		return scrollListener;
	}
	public void setScrollListener(OnScrollListener scrollListener) {
		this.scrollListener = scrollListener;
	}
}
